var fs = require("fs")
var os = require("os");
const process = require('process');
// process.exit(1);
const csvParser = require('csv-parser');
const sourceFile = './csv/clean.csv';
const tagetFile = './csv/format.csv';
const { keys } =  require('../config/key.js');

console.log('----- format format -----');
fs.appendFileSync(tagetFile, keys.join(',') + os.EOL);
let ele = {};
fs.createReadStream(sourceFile)
  .pipe(csvParser())
  .on('data', (data) => {
    if (data.time) {
      data = format(ele, data);
      writeFile(tagetFile, data);
    } 
    ele = Object.assign({}, data);
  })
  .on('end', () => {
    console.log('----- format end -----');
  });


function writeFile(filePath, data) {
    let arr = [];
    keys.forEach(val=>{
      arr.push(data[val]);
    })
    const text = arr.join(',')
	  fs.appendFileSync(filePath, text + os.EOL);
}

function format(src, tar) {
  keys.forEach(key => {
    if(tar[key] == '') {
      tar[key] = src[key] || 0;
    }
    if(tar[key]<0) {
      tar[key] = 0;
    }
  })

  return tar;
}
